package com.example.chess.ui;

import android.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.example.chess.R;
import com.example.chess.base.BaseActivity;
import com.example.chess.bean.Chess;
import com.example.chess.receiver.SettingReceiver;
import com.example.chess.utils.ExitUtils;
import com.example.chess.widgets.ChessBoardView;
import com.example.chess.widgets.RateView;
import com.example.chess.widgets.WinListener;

/**
 * 主页面
 * Created by xuyougen.
 */
public class MainActivity extends BaseActivity implements WinListener, SettingReceiver.OnSettingChangedListener {

    private int blackWinCount;
    private int whiteWinCount;

    private TextView tvBlackWinTimes;
    private TextView tvWhiteWinTimes;

    private ChessBoardView chessBoardView;
    private RateView rateView;

    @Override
    protected int setLayoutResource() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        fitWithStatusBar(R.id.titleContainer);
        tvBlackWinTimes = findViewById(R.id.tvBlackWinTimes);
        tvWhiteWinTimes = findViewById(R.id.tvWhiteWinTimes);
        chessBoardView = findViewById(R.id.chessView);
        chessBoardView.setWinListener(this);
        rateView = findViewById(R.id.rateView);

        SettingReceiver.register(this)
                .setOnSettingChangedListener(this);
    }

    @Override
    public void onNewWin(Chess.Type type) {
        if (type == Chess.Type.BLACK)
            blackWinCount++;
        else
            whiteWinCount++;
        rateView.setCount(blackWinCount, whiteWinCount);
        tvBlackWinTimes.setText(String.format("胜利%S次", blackWinCount));
        tvWhiteWinTimes.setText(String.format("胜利%S次", whiteWinCount));

        chessBoardView.setAlpha(0.5f);
        new AlertDialog.Builder(this)
                .setTitle("Tips")
                .setCancelable(false)
                .setMessage(String.format("%s胜利啦!!", type.name()))
                .setPositiveButton("确认", (dialog, which) -> {
                    dialog.dismiss();
                }).show();
    }

    public void regret(View view) {
        chessBoardView.regret();
    }

    public void newGame(View view) {
        new AlertDialog.Builder(this)
                .setTitle("Tips")
                .setMessage("确认重开一局吗?")
                .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                .setPositiveButton("确认", (dialog, which) -> {
                    dialog.dismiss();
                    chessBoardView.setAlpha(1f);
                    chessBoardView.resetChessboard();
                }).show();
    }

    public void settingPage(View view) {
        SettingActivity.start(this);
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        ExitUtils.onBackPressed(this);
    }

    @Override
    public void onSettingChanged() {
        chessBoardView.setAlpha(1f);
        chessBoardView.reloadSetting();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        chessBoardView.release();
    }
}